package com.store.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.OrderSellGoodsBean;
import com.store.app.utils.MoneyUtil;

import java.util.List;

public class SellGoodsAdapter extends BaseAdapter {
    private List<OrderSellGoodsBean> goodsList;
    private Context context;


    public SellGoodsAdapter(List<OrderSellGoodsBean> goodsList, Context context) {
        Log.v("zyl", "适配器订单数量：" + goodsList.size());
        this.goodsList = goodsList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_order_goods, null);
            viewHolder.order_title = (TextView) convertView.findViewById(R.id.order_title);
            viewHolder.tv_order_img = (ImageView) convertView.findViewById(R.id.tv_order_img);
            viewHolder.tv_order_count = (TextView) convertView.findViewById(R.id.tv_order_count);
            viewHolder.tv_order_money = (TextView) convertView.findViewById(R.id.tv_order_money);
            viewHolder.tv_order_size = (TextView) convertView.findViewById(R.id.tv_order_size);
            viewHolder.line = convertView.findViewById(R.id.line);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //viewHolder.ivGoods.setVisibility(View.VISIBLE);
        //����ͼƬ
        //viewHolder.tvGoods.setVisibility(View.GONE);

        viewHolder.order_title.setText(goodsList.get(position).getGoodsName());
        viewHolder.tv_order_count.setText("X " + goodsList.get(position).getGoodsCount());
        String type = goodsList.get(position).getType();
        if ("1".equals(type)) {
            viewHolder.tv_order_money.setText("¥ " + goodsList.get(position).getGoodsPrice());
            viewHolder.tv_order_money.setTextColor(Color.parseColor("#ff4242"));
        } else if ("2".equals(type)) {
            try{
            String str = MoneyUtil.moneyMul(goodsList.get(position).getGoodsPrice(), "10");
            str = str.split("\\.")[0];
            viewHolder.tv_order_money.setText(str + "礼券");
            }catch (Exception e){
                viewHolder.tv_order_money.setText(MoneyUtil.moneyMul(goodsList.get(position).getGoodsPrice(), "10") + "礼券");
            }
            viewHolder.tv_order_money.setTextColor(Color.parseColor("#f98116"));
        }


        viewHolder.tv_order_size.setText(goodsList.get(position).getSpecification());
        ImageLoader.getInstance().displayImage(goodsList.get(position).getPic_url(), viewHolder.tv_order_img, options);
        if (position == (goodsList.size() - 1)) {
            viewHolder.line.setVisibility(View.GONE);
        }

        return convertView;
    }


    class ViewHolder {
        private TextView order_title;//商品标题
        private ImageView tv_order_img;//商品图片
        private TextView tv_order_count;//商品数量
        private TextView tv_order_money;//商品单价
        private TextView tv_order_size;//规格
        private View line;
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.default_picture_s)
            .showImageOnFail(R.drawable.default_picture_s)
            .showStubImage(R.drawable.default_picture_s)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}
